import React, { Component } from 'react'
import PropTypes from 'prop-types'

export class SonToFather extends Component {
  constructor(props) {
    super(props)

    this.state = {}
  }

  render() {
    const { count } = this.props

    return (
      <div>
        <p>父传子数据，计数：{count}</p>
        <button onClick={(e) => this.changeCountClick(-1)}>点击修改 -1</button>
        <button onClick={(e) => this.changeCountClick(1)}>点击修改 +1</button>
      </div>
    )
  }

  // 点击修改函数：
  changeCountClick(num) {
    // 父传过来的函数
    this.props.changeCount(num)
  }
}

SonToFather.propTypes = {
  count: PropTypes.number.isRequired,
  changeCount: PropTypes.func.isRequired
}

SonToFather.defaultProps = {
  count: null
}

export default SonToFather
